﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ComponentModel;

namespace System
{
    public static class EumnExtend
    {
        public static int ToInt(this Enum value)
        {
            return Convert.ToInt32(value);
        }

        public static T ToEnum<T>(this int value)
        {
            Type type = typeof(T);
            if (type.IsEnum)
            {
                return (T)Enum.Parse(typeof(T), value.ToString());
            }
            throw new InvalidCastException("必须是枚举类型才能转换。");
        }

        public static String GetDescription(this Enum value)
        {
            FieldInfo EnumInfo = value.GetType().GetField(value.ToString());
            if (EnumInfo == null) return "";
            DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[])EnumInfo.
                GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (EnumAttributes.Length > 0)
            {
                return EnumAttributes[0].Description;
            }
            return "";
        } 
    }
}
